﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;
using System.Web;
using System.Web.Configuration;
using System.Reflection;
using System.Web.Security;

namespace ProviderLogging
{
    public class MembershipLog : MembershipProvider
    {
        #region Internal operations
        protected const string cnstInternalProviderName = "internalProviderName";
        public MembershipLog()
        {
            Logging.WriteString("MembershipLog started");
        }

        protected MembershipProvider internalProvider = null;
        protected MembershipProvider InternalProvider
        {
            get
            {
                if (internalProvider == null) internalProvider = GetInternalProvider();
                return internalProvider;
            }
        }

        #endregion

        #region Initialization
        string InternalProviderName = null;

        public override void Initialize(string name, NameValueCollection config)
        {
            _name = name;
            InternalProviderName = config[cnstInternalProviderName];

            Logging.LogOperation("Initialize", new object[] { name, config }, null, null);

        }

        protected MembershipProvider GetInternalProvider()
        {
            MembershipProvider result = null;
            try
            {
                result = Membership.Providers[InternalProviderName];

                MembershipSection membershipSect = (MembershipSection)System.Configuration.ConfigurationManager.GetSection("system.web/membership");
                ProviderSettings settings = membershipSect.Providers[InternalProviderName];
                result.Initialize(InternalProviderName, settings.Parameters);
            }
            catch (InvalidOperationException)
            {
                // Ignore, it's telling us that it's already been initialized
            }
            catch (IndexOutOfRangeException)
            {
                Logging.WriteString("Initialize failed.  Unable to locate the provider");
                throw;
            }
            catch (Exception excpt)
            {
                Logging.WriteString("GetInternalProvider Exception " + excpt.ToString());
                throw;
            }

            return result;
        }
        #endregion

        #region MembershipProvider Overrides
        public override string ApplicationName
        {
            get
            {
                string result;
                try
                {
                    result = InternalProvider.ApplicationName;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("ApplicationName!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("ApplicationName!Get", null, result, null);
                return result;
            }
            set
            {
                try
                {
                    InternalProvider.ApplicationName = value;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("ApplicationName!Set", new object[] { value }, null, excpt);
                    throw;
                }
                Logging.LogOperation("ApplicationName!Set", new object[] { value }, null, null);
            }
        }
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            bool result;
            try
            {
                result = InternalProvider.ChangePassword(username, oldPassword, newPassword);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("ChangePassword", new object[] { username, oldPassword, newPassword }, null, excpt);
                throw;
            }

            Logging.LogOperation("ChangePassword", new object[] { username, oldPassword, newPassword }, result, null);
            return result;
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            bool result;
            try
            {
                result = InternalProvider.ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("ChangePasswordQuestionAndAnswer", new object[] { username, password, newPasswordQuestion, newPasswordAnswer }, null, excpt);
                throw;
            }
            Logging.LogOperation("ChangePasswordQuestionAndAnswer", new object[] { username, password, newPasswordQuestion, newPasswordAnswer }, result, null);
            return result;
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion,
                                                  string passwordAnswer, bool isApproved, object providerUserKey,
                                                  out MembershipCreateStatus status)
        {
            MembershipUser result;
            try
            {
                result = InternalProvider.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("CreateUser", new object[] { username, password, email, passwordQuestion, passwordAnswer, isApproved, null }, null, excpt);
                throw;
            }
            Logging.LogOperation("CreateUser", new object[] { username, password, email, passwordQuestion, passwordAnswer, isApproved, status }, result, null);
            return result;

        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            bool result;
            try
            {
                result = InternalProvider.DeleteUser(username, deleteAllRelatedData);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("DeleteUser", new object[] { username, deleteAllRelatedData }, null, excpt);
                throw;
            }
            Logging.LogOperation("DeleteUser", new object[] { username, deleteAllRelatedData }, result, null);
            return result;
        }

        public override string Description
        {
            get
            {
                string result;
                try
                {
                    result = InternalProvider.Description;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("Description!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("Description!Get", null, result, null);
                return result;
            }
        }

        public override bool EnablePasswordReset
        {
            get
            {
                bool result;
                try
                {
                    result = InternalProvider.EnablePasswordReset;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("EnablePasswordReset!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("EnablePasswordReset!Get", null, result, null);
                return result;
            }
        }

        public override bool EnablePasswordRetrieval
        {
            get
            {
                bool result;
                try
                {
                    result = InternalProvider.EnablePasswordRetrieval;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("EnablePasswordRetreival!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("EnablePasswordRetreival!Get", null, result, null);
                return result;
            }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection result;
            try
            {
                result = InternalProvider.FindUsersByEmail(emailToMatch, pageIndex, pageSize, out totalRecords);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("FindUsersByEmail", new object[] { emailToMatch, pageIndex, pageSize, null }, null, excpt);
                throw;
            }
            Logging.LogOperation("FindUsersByEmail", new object[] { emailToMatch, pageIndex, pageSize, totalRecords }, result, null);

            return result;
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection result;
            try
            {
                result = InternalProvider.FindUsersByName(usernameToMatch, pageIndex, pageSize, out totalRecords);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("FindUsersByName", new object[] { usernameToMatch, pageIndex, pageSize, null }, null, excpt);
                throw;
            }
            Logging.LogOperation("FindUsersByName", new object[] { usernameToMatch, pageIndex, pageSize, totalRecords }, result, null);
            return result;
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection result;
            try
            {
                result = InternalProvider.GetAllUsers(pageIndex, pageSize, out totalRecords);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("GetAllUsers", new object[] { pageIndex, pageSize, null }, null, excpt);
                throw;
            }
            Logging.LogOperation("GetAllUsers", new object[] { pageIndex, pageSize, totalRecords }, result, null);

            return result;
        }

        public override int GetNumberOfUsersOnline()
        {
            int result;
            try
            {
                result = InternalProvider.GetNumberOfUsersOnline();
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("GetNumberOfUsersOnline", null, null, excpt);
                throw;
            }
            Logging.LogOperation("GetNumberOfUsersOnline", null, result, null);

            return result;
        }
        public override string GetPassword(string username, string answer)
        {
            string result;
            try
            {
                result = InternalProvider.GetPassword(username, answer);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("GetPassword", new object[] { username, answer }, null, excpt);
                throw;
            }
            Logging.LogOperation("GetPassword", new object[] { username, answer }, result, null);
            return result;
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            MembershipUser result;
            try
            {
                result = InternalProvider.GetUser(providerUserKey, userIsOnline);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("GetUser[providerUserKey]", new object[] { providerUserKey, userIsOnline }, null, excpt);
                throw;
            }
            Logging.LogOperation("GetUser[providerUserKey]", new object[] { providerUserKey, userIsOnline }, result, null);
            return result;
        }
        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            MembershipUser result;
            try
            {
                result = InternalProvider.GetUser(username, userIsOnline);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("GetUser[username]", new object[] { username, userIsOnline }, null, excpt);
                throw;
            }
            Logging.LogOperation("GetUser[username]", new object[] { username, userIsOnline }, result, null);
            return result;
        }

        public override string GetUserNameByEmail(string email)
        {
            string result;
            try
            {
                result = InternalProvider.GetUserNameByEmail(email);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("GetUserNameByEmail", new object[] { email }, null, excpt);
                throw;
            }
            Logging.LogOperation("GetUserNameByEmail", new object[] { email }, result, null);
            return result;
        }

        public override int MaxInvalidPasswordAttempts
        {
            get
            {
                int result;
                try
                {
                    result = InternalProvider.MaxInvalidPasswordAttempts;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("MaxInvalidPasswordAttempts!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("MaxInvalidPasswordAttempts!Get", null, result, null);
                return result;
            }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get
            {
                int result;
                try
                {
                    result = InternalProvider.MinRequiredNonAlphanumericCharacters;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("MinRequiredNonAlphanumericCharacters!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("MinRequiredNonAlphanumericCharacters!Get", null, result, null);
                return result;
            }
        }
        public override int MinRequiredPasswordLength
        {
            get
            {
                int result;
                try
                {
                    result = InternalProvider.MinRequiredPasswordLength;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("MinRequiredPasswordLength!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("MinRequiredPasswordLength!Get", null, result, null);
                return result;
            }
        }

        protected string _name = null;
        public override string Name
        {
            get
            {
                return (_name);
            }
        }

        public override int PasswordAttemptWindow
        {
            get
            {
                int result;
                try
                {
                    result = InternalProvider.PasswordAttemptWindow;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("PasswordAttemptWindow!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("PasswordAttemptWindow!Get", null, result, null);
                return result;
            }
        }
        public override MembershipPasswordFormat PasswordFormat
        {
            get
            {
                MembershipPasswordFormat result;
                try
                {
                    result = InternalProvider.PasswordFormat;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("PasswordFormat!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("PasswordFormat!Get", null, result, null);
                return result;
            }
        }
        public override string PasswordStrengthRegularExpression
        {
            get
            {
                string result;
                try
                {
                    result = InternalProvider.PasswordStrengthRegularExpression;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("PasswordStrengthRegularExpression!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("PasswordStrengthRegularExpression!Get", null, result, null);
                return result;
            }
        }
        public override bool RequiresQuestionAndAnswer
        {
            get
            {
                bool result;
                try
                {
                    result = InternalProvider.RequiresQuestionAndAnswer;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("RequiresQuestionAndAnswer!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("RequiresQuestionAndAnswer!Get", null, result, null);
                return result;
            }
        }

        public override bool RequiresUniqueEmail
        {
            get
            {
                bool result;
                try
                {
                    result = InternalProvider.RequiresUniqueEmail;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("RequiresUniqueEmail!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("RequiresUniqueEmail!Get", null, result, null);
                return result;

            }
        }
        public override string ResetPassword(string username, string answer)
        {
            string result;
            try
            {
                result = InternalProvider.ResetPassword(username, answer);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("ResetPassword", new object[] { username, answer }, null, excpt);
                throw;
            }
            Logging.LogOperation("ResetPassword", new object[] { username, answer }, result, null);
            return result;
        }
        public override bool UnlockUser(string userName)
        {
            bool result;
            try
            {
                result = InternalProvider.UnlockUser(userName);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("UnlockUser", new object[] { userName }, null, excpt);
                throw;
            }
            Logging.LogOperation("UnlockUser", new object[] { userName }, result, null);
            return result;
        }

        public override void UpdateUser(MembershipUser user)
        {
            try
            {
                UpdateUser(user);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("UpdateUser", new object[] { user }, null, excpt);
                throw;
            }
            Logging.LogOperation("UpdateUser", new object[] { user }, null, null);

        }
        public override bool ValidateUser(string username, string password)
        {
            bool result;
            try
            {
                result = InternalProvider.ValidateUser(username, password);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("ValidateUser", new object[] { username, password }, null, excpt);
                throw;
            }
            Logging.LogOperation("ValidateUser", new object[] { username, password }, result, null);
            return result;
        }

        #endregion

    }
}
